package xiecheng;

import java.util.Stack;

public class Test05
{
    public static void main(String[] args)
    {
        System.out.println(getMatchBracketsNum("[(])",2));
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * "[({}[])]",8
     * "({}])",5
     * @param inString string字符串 待判断的字符串
     * @param inStringNum int整型 输入字符串长度
     * @return int整型
     */
    public static int getMatchBracketsNum (String inString, int inStringNum) {
        // write code here
        Stack<Character> stack = new Stack<>();
        char[] array = inString.toCharArray();
        stack.push(array[0]);
        int index = 1;
        int count = 0;
        for (int i = 1;i < array.length;i++){
            if (array[i] == '(' && stack.peek() == ')'){
                stack.pop();
                count += 1;
                continue;
            }
            if (array[i] == ')' && stack.peek() == '('){
                stack.pop();
                count += 1;
                continue;
            }
            if (array[i] == '[' && stack.peek() == ']'){
                stack.pop();
                count += 2;
                continue;
            }
            if (array[i] == ']' && stack.peek() == '['){
                stack.pop();
                count += 2;
                continue;
            }
            if (array[i] == '{' && stack.peek() == '}'){
                stack.pop();
                count += 3;
                continue;
            }
            if (array[i] == '}' && stack.peek() == '{'){
                stack.pop();
                count += 3;
                continue;
            }
            stack.push(array[i]);
        }
        if (count != inStringNum){
            return -1;
        }
        return count;
    }

}
